package com.yvon.maple.service.auth.config;
import com.yvon.maple.service.auth.authentication.email.EmailAuthenticationProvider;
import com.yvon.maple.service.auth.authentication.ldap.LdapAuthenticationProvider;
import com.yvon.maple.service.auth.authentication.mobile.SmsAuthenticationProvider;
import com.yvon.maple.service.auth.service.SecurityUserService;
import com.yvon.maple.service.auth.valicode.validate.code.processor.CodeValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * SpringSecurity配置
 * Created by macro on 2019/10/8.
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityUserService securityUserService;

    @Autowired
    private CodeValidator codeValidator;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http//关闭跨站请求防护
                .cors()
                .and()
                .csrf()
                .disable().authorizeRequests()

                .antMatchers("/rsa/publicKey", "/oauth/login", "/oauth/code").permitAll()
                .antMatchers("/v2/api-docs").permitAll()
                .antMatchers("/oauth/login").permitAll()
                .antMatchers("/oauth/code").permitAll()
                .antMatchers("/oauth/validate").permitAll()

                .anyRequest().authenticated();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * @Description Spring Security认证服务中的相关实现重新定义
     * @Date 2019/7/4 17:40
     * @Version  1.0
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 加入自定义的安全认证
        auth.userDetailsService(this.securityUserService)
                .passwordEncoder(this.passwordEncoder())
                .and()
                //添加自定义的认证管理类
                .authenticationProvider(smsAuthenticationProvider())
                .authenticationProvider(emailAuthenticationProvider())
                .authenticationProvider(ldapAuthenticationProvider());
    }

    @Bean
    public AuthenticationProvider smsAuthenticationProvider(){
        return new SmsAuthenticationProvider(securityUserService, codeValidator);
    }

    @Bean
    public AuthenticationProvider emailAuthenticationProvider(){
        return new EmailAuthenticationProvider(securityUserService, codeValidator);
    }

    @Bean
    public LdapAuthenticationProvider ldapAuthenticationProvider(){
        return new LdapAuthenticationProvider(securityUserService);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}